﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AndroidTool : MonoBehaviour
{
    public delegate void AdCallBack();

    static AndroidTool _single;
    AndroidJavaClass jc;
    AndroidJavaObject jo;
    static string objName = "AndroidObj";

    /// <summary>
    /// 视屏广告
    /// </summary>
    static string gameVideo1 = "GameVideo1";

    /// <summary>
    /// 插屏广告
    /// </summary>
    static string gameVideo2 = "ChaPing";

    //Banner广告
    static string gameVideo3 = "ShowBanner";

    Dictionary<int, Delegate> actions;

    public static AndroidTool single
    {
        get
        {
            if (_single == null)
            {
                GameObject gameObject = new GameObject(objName);
                _single = gameObject.AddComponent<AndroidTool>();
                DontDestroyOnLoad(gameObject);
            }

            return _single;
        }
    }

    private void Awake()
    {
        actions = new Dictionary<int, Delegate>();
        foreach (int item in Enum.GetValues(typeof(AndroidToolEnum)))
        {
            actions.Add(item, null);
        }
#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
#endif
    }

    public void CallAdEvent(AndroidToolEnum type, AdCallBack callBack)
    {
        actions[(int)type] = callBack;
        Ad1Func(type);
    }

    void Ad1Func(AndroidToolEnum i = 0)
    {
        int index = (int)i;
        Debug.Log(string.Format("视频广告{0}", i));
#if UNITY_ANDROID && !UNITY_EDITOR &&!EVALUATING
        jo.Call(gameVideo1, index.ToString());
#endif
#if EVALUATING||UNITY_EDITOR
        AdSuccessCallBack(index.ToString());
#endif
    }

    public void Ad2Func()
    {
        Debug.Log(string.Format("插屏广告"));

#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call(gameVideo2);
#endif
    }

    public void Ad3Func()
    {
        Debug.Log(string.Format("Banner广告"));

#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call(gameVideo3);
#endif
    }

    public void KeFu()
    {
        Debug.Log("Kefu");
#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call("KeFu");
#endif
    }

    public void MoreGame()
    {
        Debug.Log("MoreGame");
#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call("MoreGame");
#endif
    }

    public void Hide()
    {
        Debug.Log("Hide");
#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call("Hide");
#endif
    }

    public void QuitGame()
    {
        Debug.Log("QuitGame");
#if UNITY_ANDROID && !UNITY_EDITOR&&!EVALUATING
        jo.Call("QuitGame");
#endif
    }

    void AdSuccessCallBack(string iStr = "0")
    {
        int i = int.Parse(iStr);
        if (actions[i] == null)
            throw new Exception(string.Format("此委托没有数据{0}", (AndroidToolEnum)i));
        else
            ((AdCallBack)actions[i])();
    }

    public void RemoveEvent(AndroidToolEnum type, AdCallBack callBack)
    {
        actions[(int)type] = (AdCallBack)actions[(int)type] - callBack;
    }
}

public enum AndroidToolEnum
{
    None = 0,
    OnAdd = 1,
}